package com.toman.ui.work.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.R;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.toolbar.ToolBarActivity;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

public class WorkcalendarActivity extends ToolBarActivity  implements
        CalendarView.OnDateSelectedListener,
        CalendarView.OnMonthChangeListener,
        CalendarView.OnYearChangeListener,
        CalendarView.OnDateLongClickListener
       {
    final List<Calendar> schemes = new ArrayList<>();

    TextView mTextMonthDay;

    TextView mTextYear;

    TextView mTextLunar;

    TextView mTextCurrentDay;

    CalendarView mCalendarView;

    RelativeLayout mRelativeTool;
    private int mYear;
           private int nYear,nMonth;
    CalendarLayout mCalendarLayout;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workcalendar);
        initToolBar("工作日历");
        initView();

    }

    private void initView() {

        mTextMonthDay = (TextView) findViewById(R.id.tv_month_day);
        mTextYear = (TextView) findViewById(R.id.tv_year);
        mTextLunar = (TextView) findViewById(R.id.tv_lunar);
        mRelativeTool = (RelativeLayout) findViewById(R.id.rl_tool);
        mCalendarView = (CalendarView) findViewById(R.id.calendarView);
        mTextCurrentDay = (TextView) findViewById(R.id.tv_current_day);
        mTextMonthDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mCalendarLayout.isExpand()) {
                    mCalendarView.showYearSelectLayout(mYear);
                    return;
                }
                mCalendarView.showYearSelectLayout(mYear);
                mTextLunar.setVisibility(View.GONE);
                mTextYear.setVisibility(View.GONE);
                mTextMonthDay.setText(String.valueOf(mYear));
            }
        });
        findViewById(R.id.fl_current).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendarView.scrollToCurrent();
            }
        });

        mCalendarLayout = (CalendarLayout) findViewById(R.id.calendarLayout);
        mCalendarView.setOnYearChangeListener(this);
//        mCalendarView.setOnDateSelectedListener(this);
        mCalendarView.setOnMonthChangeListener(this);
        mCalendarView.setOnDateLongClickListener(this, true);
        mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
        mYear = mCalendarView.getCurYear();
        mTextMonthDay.setText(mCalendarView.getCurMonth() + "月" + mCalendarView.getCurDay() + "日");
        mTextLunar.setText("今日");
        mTextCurrentDay.setText(String.valueOf(mCalendarView.getCurDay()));

    }

    protected void initData() {
        final int year = mCalendarView.getCurYear();
        final int month = mCalendarView.getCurMonth();
//        schemes.add(getSchemeCalendar(year, month, 3, 0xFF40db25, "假"));
//        schemes.add(getSchemeCalendar(year, month, 6, 0xFFe69138, "事"));
//        schemes.add(getSchemeCalendar(year, month, 9, 0xFFdf1356, "议"));
//        schemes.add(getSchemeCalendar(year, month, 13, 0xFFedc56d, "记"));
//        schemes.add(getSchemeCalendar(year, month, 14, 0xFFedc56d, "记"));
//        schemes.add(getSchemeCalendar(year, month, 15, 0xFFaacc44, "假"));
//        schemes.add(getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记"));
//        schemes.add(getSchemeCalendar(year, month, 25, 0xFF13acf0, "假"));

        mCalendarView.setSchemeDate(schemes);

    }
    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onDateSelected(Calendar calendar, boolean isClick) {
        Log.e("onDateSelected", "  -- " + calendar.getYear() + "  --  " + calendar.getMonth() + "  -- " + calendar.getDay());
        mTextLunar.setVisibility(View.VISIBLE);
        mTextYear.setVisibility(View.VISIBLE);
        mTextMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
        mTextYear.setText(String.valueOf(calendar.getYear()));
        mTextLunar.setText(calendar.getLunar());
        mYear = calendar.getYear();
        if (isClick) {
            Toast.makeText(this, getCalendarText(calendar), Toast.LENGTH_SHORT).show();
        }

//        Toast.makeText(this, "长按不选择日期\n" + getCalendarText(calendar), Toast.LENGTH_SHORT).show();
        if (schemes.contains(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), 0xFF13acf0, "假"))){
            schemes.remove(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), 0xFF13acf0, "假"));}
        else
        { schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), 0xFF13acf0, "假"));}

        mCalendarView.setSchemeDate(schemes);
    }

    @Override
    public void onDateLongClick(Calendar calendar) {
////        Toast.makeText(this, "长按不选择日期\n" + getCalendarText(calendar), Toast.LENGTH_SHORT).show();
//        if (schemes.contains(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), 0xFF13acf0, "假"))){
//        schemes.remove(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), 0xFF13acf0, "假"));}
//        else
//        { schemes.add(getSchemeCalendar(calendar.getYear(), calendar.getMonth(), calendar.getDay(), 0xFF13acf0, "假"));}
//
//        mCalendarView.setSchemeDate(schemes);
        for (int i=0;i<schemes.size();i++){
            System.out.println(schemes.get(i).getDay());
        }


    }

    private static String getCalendarText(Calendar calendar) {
        return String.format("新历%s \n 农历%s \n 公历节日：%s \n 农历节日：%s \n 节气：%s \n 是否闰月：%s",
                calendar.getMonth() + "月" + calendar.getDay() + "日",
                calendar.getLunarCakendar().getMonth() + "月" + calendar.getLunarCakendar().getDay() + "日",
                TextUtils.isEmpty(calendar.getGregorianFestival()) ? "无" : calendar.getGregorianFestival(),
                TextUtils.isEmpty(calendar.getTraditionFestival()) ? "无" : calendar.getTraditionFestival(),
                TextUtils.isEmpty(calendar.getSolarTerm()) ? "无" : calendar.getSolarTerm(),
                calendar.getLeapMonth() == 0 ? "否" : String.format("闰%s月", calendar.getLeapMonth()));
    }

    @Override
    public void onMonthChange(int year, int month) {
if (month<10){
    customer(year+"-"+"0"+month);
}
   else {     customer(year+"-"+month);}
//        schemes.add(getSchemeCalendar(year, month, 3, 0xFF40db25, "假"));
//        schemes.add(getSchemeCalendar(year, month, 6, 0xFFe69138, "事"));
//        schemes.add(getSchemeCalendar(year, month, 9, 0xFFdf1356, "议"));
//        schemes.add(getSchemeCalendar(year, month, 13, 0xFFedc56d, "记"));
//        schemes.add(getSchemeCalendar(year, month, 14, 0xFFedc56d, "记"));
//        schemes.add(getSchemeCalendar(year, month, 15, 0xFFaacc44, "假"));
//        schemes.add(getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记"));
//        schemes.add(getSchemeCalendar(year, month, 25, 0xFF13acf0, "假"));
        nMonth=month;
nYear=year;
        mCalendarView.setSchemeDate(schemes);

    }

    @Override
    public void onYearChange(int year) {
        mTextMonthDay.setText(String.valueOf(year));
    }

    /*
    企业列表
 */
    public void customer(String month) {
ToastUtil.print(month+"-"+getIntent().getStringExtra("id"));
        mTextMonthDay.setText(month);

        OkHttpUtils
                .get(NewBaseUrl.workcalendar_month)
                .params("token", token)
                .params("departmentId",getIntent().getStringExtra("id"))
                .params("month", month)

                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "工作日历: " + result);
                      
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code =jsonObj.getInteger("code");
                        ViewUtil.lagout(code,getApplicationContext());
                        if (code==0){
String data=    jsonObj.getString("data");
if (!data.equals("")){
    String[] date = data.split(",");
for (int i=0;i<date.length;i++){
          if (!date[i].equals(""))
            schemes.add(getSchemeCalendar(nYear, nMonth, Integer.parseInt(date[i]), 0xFF40db25, "班"));

}
    mCalendarView.setSchemeDate(schemes);

}
                        }

                        else {
                            ToastUtil.makeText(getApplicationContext(),jsonObj.getString("msg"));
                        }

                    }
                });
    }
}
